/**
 * fileName:  NutritionImpl.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-4 下午10:28:32
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-4          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;
import java.util.List;

import org.cloudland.commercial.masterdata.bean.MineralInfo;
import org.cloudland.commercial.masterdata.bean.NutrientInfo;
import org.cloudland.commercial.masterdata.bean.NutritionType;
import org.cloudland.commercial.masterdata.bean.VitamiInfo;
import org.cloudland.commercial.masterdata.handler.NutritionStore;
import org.cloudland.commercial.masterdata.store.PageResult;
import org.cloudland.commercial.masterdata.store.entity.NutritionEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.commercial.masterdata.store.read.NutrientReader;
import org.cloudland.commercial.masterdata.store.write.NutrientWriter;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  NutritionImpl
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class NutritionImpl implements NutritionStore {
	
	/**
	 * nutrientWriter 营养信息写入数据库操作
	 */
	private NutrientWriter nutrientWriter;
	
	/**
	 * nutrientReader 营养信息读取数据库操作
	 */
	private NutrientReader nutrientReader;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public NutritionImpl(NutrientWriter nutrientWriter, NutrientReader nutrientReader) {
		this.nutrientWriter = nutrientWriter;
		this.nutrientReader = nutrientReader;
	}
	
	/**
	 * <pre>
	 * 
	 * </pre>
	 *
	 * @param id
	 * @return   
	 */
	private NutritionBean findNutritionInfoById(String id) {
		NutritionEntity entity = nutrientReader.findNutrientById(id);
		return new NutritionBean(entity, nutrientWriter);
	}

	/**
	 * @return
	 */
	public NutrientInfo createNutrientInfo() {
		return new NutritionBean(NutritionType.NUTRIENT, nutrientWriter);
	}

	/**
	 * @param nutrientId
	 * @return
	 */
	public NutrientInfo findNutrientInfoById(String nutrientId) {
		NutritionBean bean = findNutritionInfoById(nutrientId);
		if (!NutritionType.NUTRIENT.equals(bean.getType())) {
			bean = null;
		}
		return bean;
	}

	/**
	 * @return
	 */
	public NutrientInfo[] findAllNutrientInfo() {
		List<NutritionEntity> list = nutrientReader.findAllNutrition(NutritionType.NUTRIENT.code());
		if(null == list)
		{
			return null;
		}
		NutrientInfo[] ninfo = new NutrientInfo[list.size()];
		for(int i = 0; i < list.size(); i++)
		{
			ninfo[i] = new NutritionBean(list.get(i),nutrientWriter);
		}
		return ninfo;
	}
	
	/**
	 * @param startNo
	 * @param pageSize
	 * @return
	 */
	public PageResult<NutrientInfo> findPageNutrientInfo(Integer startNo, Integer pageSize) {
		
		PageQueryEntity<NutritionEntity> entityArray = nutrientReader.findNutrientForPage(NutritionType.NUTRIENT.code(), startNo, pageSize);
		PageQueryEntity<NutrientInfo> pageResult = new PageQueryEntity<NutrientInfo>(entityArray.count(), new ArrayList<NutrientInfo>());
		for (NutritionEntity entity : entityArray.list()) {
			pageResult.list().add(new NutritionBean(entity, nutrientWriter));
		}
		
		return pageResult;
	}

	/**
	 * @param nutrientId
	 * @return
	 */
	public Boolean removeNutrientInfo(String nutrientId) {
		NutritionEntity entityDao = new NutritionEntity(nutrientId, "", "", 0, NutritionType.NUTRIENT);
		nutrientWriter.delete(entityDao);
		return true;
	}

	/**
	 * @return
	 */
	public VitamiInfo createVitamiInfo() {
		return new NutritionBean(NutritionType.VITAMI, nutrientWriter);
	}

	/**
	 * @param vitamiId
	 * @return
	 */
	public VitamiInfo findVitamiInfoById(String vitamiId) {
		NutritionBean bean = findNutritionInfoById(vitamiId);
		if (!NutritionType.VITAMI.equals(bean.getType())) {
			bean = null;
		}
		return bean;
	}

	/**
	 * @return
	 */
	public VitamiInfo[] findAllVitamiInfo() {
		List<NutritionEntity> list = nutrientReader.findAllNutrition(NutritionType.VITAMI.code());
		if(null == list)
		{
			return null;
		}
		VitamiInfo[] vinfo = new VitamiInfo[list.size()];
		for(int i = 0; i < list.size(); i++)
		{
			vinfo[i] = new NutritionBean(list.get(i),nutrientWriter);
		}
		return vinfo;
	}
	
	/**
	 * 
	 * @param startNo
	 * @param pageSize
	 * @return
	 */
	public PageResult<VitamiInfo> findPageVitamiInfo(Integer startNo, Integer pageSize) {
		PageQueryEntity<NutritionEntity> entityArray = nutrientReader.findNutrientForPage(NutritionType.VITAMI.code(), startNo, pageSize);
		PageQueryEntity<VitamiInfo> pageResult = new PageQueryEntity<VitamiInfo>(entityArray.count(), new ArrayList<VitamiInfo>());
		for (NutritionEntity entity : entityArray.list()) {
			pageResult.list().add(new NutritionBean(entity, nutrientWriter));
		}
		
		return pageResult;
	}

	/**
	 * @param vitamiId
	 * @return
	 */
	public Boolean removeVitamiInfo(String vitamiId) {
		NutritionEntity entityDao = new NutritionEntity(vitamiId, "", "", 0, NutritionType.VITAMI);
		nutrientWriter.delete(entityDao);
		return true;
	}

	/**
	 * @return
	 */
	public MineralInfo createMineralInfo() {
		return new NutritionBean(NutritionType.MINERAL, nutrientWriter);
	}

	/**
	 * @param mineralId
	 * @return
	 */
	public MineralInfo findMineralInfoById(String mineralId) {
		NutritionBean bean = findNutritionInfoById(mineralId);
		if (!NutritionType.MINERAL.equals(bean.getType())) {
			bean = null;
		}
		return bean;
	}

	/**
	 * @return
	 */
	public MineralInfo[] findAllMineralInfo() {
		List<NutritionEntity> list = nutrientReader.findAllNutrition(NutritionType.MINERAL.code());
		if(null == list)
		{
			return null;
		}
		MineralInfo[] minfo = new MineralInfo[list.size()];
		for(int i = 0; i < list.size(); i++)
		{
			minfo[i] = new NutritionBean(list.get(i),nutrientWriter);
		}
		return minfo;
	}

	/**
	 * @param startNo
	 * @param pageSize
	 * @return
	 */
	public PageResult<MineralInfo> findPageMineralInfo(Integer startNo, Integer pageSize) {
		PageQueryEntity<NutritionEntity> entityArray = nutrientReader.findNutrientForPage(NutritionType.MINERAL.code(), startNo, pageSize);
		PageQueryEntity<MineralInfo> pageResult = new PageQueryEntity<MineralInfo>(entityArray.count(), new ArrayList<MineralInfo>());
		for (NutritionEntity entity : entityArray.list()) {
			pageResult.list().add(new NutritionBean(entity, nutrientWriter));
		}
		
		return pageResult;
	}
	
	/**
	 * @param mineralId
	 * @return
	 */
	public Boolean removeMineralInfo(String mineralId) {
		NutritionEntity entityDao = new NutritionEntity(mineralId, "", "", 0, NutritionType.MINERAL);
		nutrientWriter.delete(entityDao);
		return true;
	}

}
